#include <string.h>
#include <iostream>

using std::cout;
using std::endl;


class Base
{
public:
    Base()
    //: _base(0)
    {   cout << "Base()" << endl;}

    Base(const char * base)
    : _base(new char[strlen(base) + 1]())
    {   
        cout << "Base(const char *)" << endl;
        strcpy(_base, base);
    }

    Base(const Base & rhs)
    : _base(new char[strlen(rhs._base) + 1]())
    {   
        cout << "Base(const Base &)" << endl;   
        strcpy(_base, rhs._base);
    }

    Base & operator=(const Base & rhs)
    {
        cout << "Base & opeartor=(const Base &)" << endl;
        if(this != &rhs) {
            delete [] _base;
            _base = new char[strlen(rhs._base) + 1]();
            strcpy(_base, rhs._base);
        }
        return *this;
    }

    ~Base() {   
        cout << "~Base()" << endl;  
        if(_base) {
            delete [] _base;
            _base = nullptr;
        }
    }
    
    void print() const
    {   cout << "Base::_base:" << _base << endl;    }
private:
    char * _base;
};

class Derived
: public Base
{
public:
    Derived(const char * base, long derived)
    : Base(base)
    , _derived(derived)
    {   cout << "Derived(long,long)" << endl;}

    ~Derived() {    cout << "~Derived()" << endl;}

    //1. 派生类内部没有显式定义复制控制函数（拷贝构造函数、赋值运算符函数）
    //系统会提供，同时基类部分的复制控制函数会被自动调用

    void display() const
    {
        cout << "_derived:" << _derived << endl;
    }

private:
    long _derived;
};

int main()
{
    Derived d1("hello", 100);
    d1.print();
    d1.display();

    Derived d2("wangdao", 2);   
    cout << "d2:" << endl;
    d2.print();
    d2.display();
    cout << endl << "执行赋值操作以后:" << endl;
    d2 = d1;
    d2.print();
    d2.display();

    cout << "执行拷贝构造:" << endl;
    Derived d3 = d2;
    cout << "d3:";
    d3.print();
    d3.display();


    return 0;
}

